<template>
  <div>
    <img src="../assets/img/shuhui.png" alt="" class="logo">
    <div class="form">
      <form action="" @submit.prevent="submit">
        <div>
          <my-input name="name" placeholder="邮箱地址" v-model="user.mail" pattern="^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$">
            <i class="fa fa-envelope-o"></i>
          </my-input>
          <hr>
          <my-input name="password" type="password" placeholder="密码" v-model="user.password" pattern="^[\w]{6,16}$">
            <i class="fa fa-lock"></i>
          </my-input>
          <hr>
          <my-input name="repassword" type="password" placeholder="确认密码" v-model="user.rePassword" pattern="^[\w]{6,16}$">
            <i class="fa fa-lock"></i>
          </my-input>
        </div>
        <button :disabled="dis" :class="{success:!dis}">注册</button>
        <!--<el-button type="text" @click="open">
            点击打开 Message Box
        </el-button>-->
      </form>
    </div>
    <div class="login-footer">
      <router-link to="/users">
        <span class="border-right">登录</span>
      </router-link>
      <router-link to="/forget">
        <span>忘记密码</span>
      </router-link>
    </div>
  </div>
</template>
<script>
  import MyInput from '../components/MyInput'
  import bus from '../bus.js'
  export default {
    data() {
      return {
        user: {
          mail: "",
          password: "",
          rePassword: ""
        },
        dis: true,
        errtext:""
      }
    },
    components: {
      MyInput
    },
    methods: {
      mychange() {
        if (this.user.mail.length != 0 && this.user.password.length != 0 && this.user.password == this.user.rePassword) {
          this.dis = false
        } else {
          this.dis = true
        }
      },
      submit() {
        this.axios.post('http://u.ishuhui.com/join', this.user)
          .then(data => {
            if (data.data.data) {
              this.$alert('恭喜，注册成功', '注册成功', {
                confirmButtonText: '确定',
                callback: action => {
                  this.$router.push({
                    path: '/users'
                  })
                }
              });
            }else{
              if(data.data.errMsg.mail){
                this.errtext = data.data.errMsg.mail
                this.$message.error(this.errtext);
              }else{
                this.$message.error('两次密码不一致');
              }
            }
          })
          .catch(err => {
            console.log(err)
            console.log('???')
          })
      }
    },
    mounted() {
      bus.$on('changes', this.mychange)
    },
  }

</script>
<style scoped>
  .logo {
    width: 50%;
    display: block;
    margin: 50px auto;
  }
  
  .form {
    width: 100%;
    font-size: .6rem;
  }
  
  .form i {
    color: #ccc;
  }
  
  .form form>div {
    width: 100%;
    padding: 15px;
    background-color: white;
    border-top: 1px solid #ccc;
    border-bottom: 1px solid #ccc;
  }
  
  hr {
    background-color: #ccc;
    margin: 10px 0;
  }
  
  .form button {
    width: 90%;
    height: 50px;
    display: block;
    margin: 20px auto;
    line-height: 50px;
    text-align: center;
    font-size: .5rem;
    border: 1px solid #ccc;
    border-radius: 5px;
  }
  
  .success {
    color: white;
    background-color: #ff4400;
  }
  
  .login-footer {
    width: 100%;
    text-align: center;
    position: absolute;
    bottom: 100px;
  }

  .login-footer span {
    padding: 0 20px;
    font-size: .4rem;
    color: #a39e9e;
  }
  
  .border-right {
    border-right: 1px solid #ccc;
  }

</style>
